﻿using AiXiu.Common;
using AiXiu.DAL;
using AiXiu.IBLL;
using AiXiu.Model;
using System;
using System.Collections.Generic;

namespace AiXiu.BLL
{
    /// <summary>
    /// 评论业务管理类
    /// </summary>
    public class DiscussManager: IDiscussManager
    {
        private const string keyPrefix = "VideoDiscuss";

        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="videoId">视频Id</param>
        /// <param name="discuss">评论</param>
        public bool Add(string videoId, Discuss discuss)
        {
            string key = $"{keyPrefix}_{videoId}";
            long score = TimeHelper.ConvertDateTimeByUnix(DateTime.Now);
            return RedisHelper.SortedSetAdd(key, discuss, score);


        }

        /// <summary>
        /// 评论计数
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public long Count(string videoId)
        {
            string key = $"{keyPrefix}_{videoId}";
            return RedisHelper.SortedSetLength(key);
        }

        /// <summary>
        /// 查询评论列表
        /// </summary>
        /// <param name="videoId"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<Discuss> QueryDiscusses(string videoId, int pageNumber, int pageSize = 10)
        {
            string key = $"{keyPrefix}_{videoId}";
            int start = (pageNumber - 1) * pageSize;
            int stop = start + pageSize - 1;
            return RedisHelper.SortedSetRangeByRank<Discuss>(key, start, stop, false);
        }
    }
}